package compare

type Comparator func(a, b interface{}) int

type Equaler func(a, b interface{}) bool

func GetCmp(e interface{}) (cmp Comparator) {
	if e == nil {
		return nil
	}
	switch e.(type) {
	case bool:
		return boolCmp
	case int:
		return intCmp
	case int8:
		return int8Cmp
	case uint8:
		return uint8Cmp
	case int16:
		return int16Cmp
	case uint16:
		return uint16Cmp
	case int32:
		return int32Cmp
	case uint32:
		return uint32Cmp
	case int64:
		return int64Cmp
	case uint64:
		return uint64Cmp
	case float32:
		return float32Cmp
	case float64:
		return float64Cmp
	case complex64:
		return complex64Cmp
	case complex128:
		return complex128Cmp
	case string:
		return stringCmp
	}
	return nil
}

//以下为系统自带类型的默认比较器

func boolCmp(a, b interface{}) int {
	if a == b {
		return 0
	}
	if a.(bool) {
		return 1
	} else if b.(bool) {
		return -1
	}
	return 0
}
func intCmp(a, b interface{}) int {
	if a == b {
		return 0
	}
	if a.(int) > b.(int) {
		return 1
	} else if a.(int) < b.(int) {
		return -1
	}
	return 0
}
func int8Cmp(a, b interface{}) int {
	if a == b {
		return 0
	}
	if a.(int8) > b.(int8) {
		return 1
	} else if a.(int8) < b.(int8) {
		return -1
	}
	return 0
}
func uint8Cmp(a, b interface{}) int {
	if a == b {
		return 0
	}
	if a.(uint8) > b.(uint8) {
		return 1
	} else if a.(uint8) < b.(uint8) {
		return -1
	}
	return 0
}
func int16Cmp(a, b interface{}) int {
	if a == b {
		return 0
	}
	if a.(int16) > b.(int16) {
		return 1
	} else if a.(int16) < b.(int16) {
		return -1
	}
	return 0
}
func uint16Cmp(a, b interface{}) int {
	if a == b {
		return 0
	}
	if a.(uint16) > b.(uint16) {
		return 1
	} else if a.(uint16) < b.(uint16) {
		return -1
	}
	return 0
}
func int32Cmp(a, b interface{}) int {
	if a == b {
		return 0
	}
	if a.(int32) > b.(int32) {
		return 1
	} else if a.(int32) < b.(int32) {
		return -1
	}
	return 0
}
func uint32Cmp(a, b interface{}) int {
	if a == b {
		return 0
	}
	if a.(uint32) > b.(uint32) {
		return 1
	} else if a.(uint32) < b.(uint32) {
		return -1
	}
	return 0
}
func int64Cmp(a, b interface{}) int {
	if a == b {
		return 0
	}
	if a.(int64) > b.(int64) {
		return 1
	} else if a.(int64) < b.(int64) {
		return -1
	}
	return 0
}
func uint64Cmp(a, b interface{}) int {
	if a == b {
		return 0
	}
	if a.(uint64) > b.(uint64) {
		return 1
	} else if a.(uint64) < b.(uint64) {
		return -1
	}
	return 0
}
func float32Cmp(a, b interface{}) int {
	if a == b {
		return 0
	}
	if a.(float32) > b.(float32) {
		return 1
	} else if a.(float32) < b.(float32) {
		return -1
	}
	return 0
}
func float64Cmp(a, b interface{}) int {
	if a == b {
		return 0
	}
	if a.(float64) > b.(float64) {
		return 1
	} else if a.(float64) < b.(float64) {
		return -1
	}
	return 0
}
func complex64Cmp(a, b interface{}) int {
	if a == b {
		return 0
	}
	if real(a.(complex64)) > real(b.(complex64)) {
		return 1
	} else if real(a.(complex64)) < real(b.(complex64)) {
		return -1
	} else {
		if imag(a.(complex64)) > imag(b.(complex64)) {
			return 1
		} else if imag(a.(complex64)) < imag(b.(complex64)) {
			return -1
		}
	}
	return 0
}
func complex128Cmp(a, b interface{}) int {
	if a == b {
		return 0
	}
	if real(a.(complex128)) > real(b.(complex128)) {
		return 1
	} else if real(a.(complex128)) < real(b.(complex128)) {
		return -1
	} else {
		if imag(a.(complex128)) > imag(b.(complex128)) {
			return 1
		} else if imag(a.(complex128)) < imag(b.(complex128)) {
			return -1
		}
	}
	return 0
}
func stringCmp(a, b interface{}) int {
	if a == b {
		return 0
	}
	if len(a.(string)) > len(b.(string)) {
		return 1
	} else if len(a.(string)) < len(b.(string)) {
		return -1
	} else {
		if a.(string) > b.(string) {
			return 1
		} else if a.(string) < b.(string) {
			return -1
		}
	}
	return 0
}

func GetEqual() (equ Equaler) {
	return basicEqual
}

func basicEqual(a, b interface{}) (B bool) {
	return a == b
}
